﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ChangeItemCtrl" in code, svc and config file together.
public class ChangeItemCtrl : IChangeItemCtrl
{
    public void DoWork()
    {
    }

    public List<Item> GetAllItem()
    {
        List<Item> listItem = new List<Item>();
        DataClassesDataContext db = new DataClassesDataContext();
        List<Item_Table> items = db.Item_Tables.ToList();
        foreach (Item_Table ite in items)
        {
            Item i = new Item(ite);
            listItem.Add(i);
        }
        return listItem;
    }

    public void UpdateItem(Guid itemID, String itemTitle, String itemDescription, String itemImage, String itemTag, Boolean bidStat, DateTime bidStartDate, DateTime bidEndDate, int bidIncremement, float minimumBid, Guid categoryID)
    {
        try
        {
            DataClassesDataContext db = new DataClassesDataContext();
            var data = from tab in db.Item_Tables
                       where tab.Item_ID.Equals(itemID)
                       select tab;
            if (data.ToList().Count == 1)
            {
                foreach (var item in data)
                {
                    itemID = Guid.NewGuid();
                    item.Item_ID = itemID;
                    item.Item_Title = itemTitle;
                    item.Item_Description = itemDescription;
                    item.Item_Image = itemImage;
                    item.Item_Tag = itemTag;
                    item.Bid_Status = bidStat;
                    item.Bid_StartDate = bidStartDate;
                    item.Bid_EndDat = bidEndDate;
                    item.Bid_Incremenent = bidIncremement;
                    item.Minimum_Bid = minimumBid;
                    item.Category_ID = categoryID;
                    db.SubmitChanges();
                }
            }
        }
        catch (SqlException ex)
        {
            // Handle more specific SqlException exception here.
        }
        catch (Exception ex)
        {
            // Handle generic ones here.
        }
    }
}
